package Apaper;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

class PaperReviewSystem {

    Scanner scanner = new Scanner(System.in);
    private List<Paper> papers;
    private List<Expert> experts;
    private Random random;

    public PaperReviewSystem() throws IOException {
        this.papers = new ArrayList<>();
        this.experts = new ArrayList<>();
        this.random = new Random();
    }

    FileOutputStream fos;

    {
        try {
            fos = new FileOutputStream("D:\\论文评改.txt");
        } catch (IOException e) {
            throw new IOException(e);
        }
    }

    public void addPaper(Paper paper) {
        papers.add(paper);
    }

    public void addExpert(Expert expert) {
        experts.add(expert);
    }
    public void getPaper(){
        for (int i =0;i<papers.size();i++){
            System.out.println(papers.get(i).getTitle());
        }
    }

    public  void assignPapersToExperts() throws IOException {

        for (Paper paper : papers) {
            List<Expert> assignedExperts = getRandomExperts(3,experts);
            System.out.println(paper.getTitle()+"论文分配给：");

            for (Expert expert : assignedExperts) {
               paper.addExpert(expert.getName());
            }
            int count = 0;
            for (int i = 0; i < 3; i++) {
                if (countUnqualifiedExperts(paper, assignedExperts.get(i)) >= 60) {
                    count++;
                }
            }
            if (count==3){
                paper.setQualified(true);
            }
            else if (count==2){
                System.out.println("这篇论文需要复查");
                assignSecondReview(paper,assignedExperts);
            }
            else {
                paper.setQualified(false);
            }
        }
    }


    private void assignSecondReview(Paper paper,List<Expert> used) throws IOException{
        List<Expert> remained = new ArrayList<>();
        for (int j = 0; j < 5; j++) {
            for (int i = 0; i < 3; i++) {
                if (!experts.get(j).getName().equals(used.get(i).getName())){
                    remained.add(experts.get(j));
                }
            }
        }
        List<Expert> expert = getRandomExperts(2,remained);
        System.out.println(paper.getTitle()+"论文重新分配给：");
        for (Expert expert1 : expert) {
            paper.addExpert(expert1.getName());
        }
        int reviewcount = 0;
        for (int i = 0; i < 2; i++) {
            if (countUnqualifiedExperts(paper, expert.get(i)) >= 60) {
                reviewcount++;
            }
        }
            if (reviewcount==2){
                paper.SetReview(true);

            } else {
                paper.setQualified(false);
            }
    }

    private List<Expert> getRandomExperts(int count,List<Expert> experts) {
        List<Expert> randomExperts = new ArrayList<>();
        int totalExperts = experts.size();
        while (randomExperts.size() < count) {
            Expert expert = experts.get(random.nextInt(totalExperts));
            if (!randomExperts.contains(expert)) {
                randomExperts.add(expert);
            }
        }
        return randomExperts;
    }

    private int countUnqualifiedExperts(Paper paper,Expert expert) throws IOException {

            System.out.println("请输入"+expert.getName()+"专家的分数");
            int count = scanner.nextInt();
        String content1 = "论文《"+paper.getTitle()+"》被"+expert.getName()+"专家打了"+count+"分\n\n";
        byte[] byte2 = content1.getBytes();
        fos.write(byte2);
        return count;
    }



    public int countUnqualifiedPapers() {
        int count = 0;
        for (Paper paper : papers) {
            if (!paper.isQualified()&&!paper.isReview()) {
                count++;
            }
        }
        return count;
    }

    public int countPapersWithSingleUnqualifiedExpert() {
        int count = 0;
        for (Paper paper : papers) {
            if (paper.isReview()) {
                count++;
            }
        }
        return count;
    }

    public int countFullyQualifiedPapers() {
        int count = 0;
        for (Paper paper : papers) {
            if (paper.isQualified()) {
                count++;
            }
        }
        return count;
    }

    public int size(){
        return papers.size();
    }
}
